package com.yuanli.cosmeticmirror.model.impl;

import android.text.TextUtils;
import android.util.Log;

import androidx.lifecycle.LifecycleOwner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.comenjoysoft.baselibrary.listener.CallbackListener;
import com.orhanobut.logger.Logger;
import com.rxjava.rxlife.BaseScope;
import com.rxjava.rxlife.RxLife;
import com.yuanli.cosmeticmirror.bean.RegisterBean;
import com.yuanli.cosmeticmirror.bean.SMSBean;
import com.yuanli.cosmeticmirror.model.biz.IFindPwdBiz;
import com.yuanli.cosmeticmirror.utils.RSAUtils;

import java.util.HashMap;

import rxhttp.wrapper.param.RxHttp;

import static com.yuanli.cosmeticmirror.base.HttpApi.GET_CODE;
import static com.yuanli.cosmeticmirror.base.HttpApi.POST_FIND_PWD;
import static com.yuanli.cosmeticmirror.base.HttpApi.REGISTER;
import static com.yuanli.cosmeticmirror.base.MyApp.TAG;

public class FindPwdImpl extends BaseScope implements IFindPwdBiz {
    public FindPwdImpl(LifecycleOwner owner) {
        super(owner);
    }

    @Override
    public void getCodeModel(String phone, CallbackListener<String> listener) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("mobile", phone);
        hashMap.put("appname", "化妆镜");
        JSONObject jsonObj = new JSONObject(hashMap);
        Log.d(TAG, "getCodeModel: json phone" + jsonObj.toString());
        String postSign = RSAUtils.getSign(jsonObj.toString());
        HashMap<String, Object> hashMap2 = new HashMap<>();
        hashMap2.put("sign", postSign);
        JSONObject json = new JSONObject(hashMap2);
        Log.d(TAG, "getCodeModel: json postSign" + json.toString());
        RxHttp.postJson(GET_CODE)
                .addAll(json.toJSONString())//发送Json字符串单形式的Post请求
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    SMSBean response = JSON.parseObject(s, SMSBean.class);
                    Log.d(TAG, "getCodeModel onSuccess: " + response.getYzm());
                    if (!TextUtils.isEmpty(response.getYzm())) {
                        listener.success(response.getYzm());
                    } else {
                        listener.failed(null);
                    }
                }, throwable -> {
                    //请求失败
                    listener.failed(throwable.getMessage());
                });
    }

    @Override
    public void findPwd(String phone, String password, CallbackListener<String> listener) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("User_Phone", phone);
        hashMap.put("User_Pwd", password);
        JSONObject json = new JSONObject(hashMap);
        Log.d(TAG, "findPwd json " + json.toString() + "      ----->url" + REGISTER);
        RxHttp.postJson(POST_FIND_PWD)
                .addAll(json.toJSONString())//发送Json字符串单形式的Post请求
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    RegisterBean response = JSON.parseObject(s, RegisterBean.class);
                    Logger.d(" findPwd onSuccess: " + s);
                    if (!TextUtils.isEmpty(response.getCreateState())) {
                        listener.success(response.getCreateState());
                    } else {
                        listener.failed(null);
                                        }
                }, throwable -> {
                    Logger.d(" findPwd onFailed: " + throwable.getMessage());
                    //请求失败
                    listener.failed(throwable.getMessage());
                });
    }


}
